package cn.itsource.pet.service.impl;

import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.service.IBaiduAiAuditService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.LoginContext;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.PetDetail;
import cn.itsource.pet.domain.PetOnlineAuditLog;
import cn.itsource.pet.mapper.PetDetailMapper;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.mapper.PetOnlineAuditLogMapper;
import cn.itsource.pet.service.IPetService;
import cn.itsource.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Override
    public void save(Pet pet) {
        super.save(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.updatePetDetailByPetId(petDetail);
        }
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
        petDetailMapper.delPetDetailByPetId(id);
    }

    @Override
    public void offsale(Long[] ids) {
        Date offsaletime = new Date();
        petMapper.offsale(offsaletime, ids);
    }

    @Override
    public void onsale(Long[] ids, HttpServletRequest request) {
        //  获取登录信息对象
        Logininfo logininfo = LoginContext.getLoginUser(request);
        // 获取审核人
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            // 对文本和图片进行审核
            Boolean textCensor = baiduAiAuditService.textAudit(pet.getName());
            Boolean imgCensor = baiduAiAuditService.imageAudit(pet.getResources());
            if (textCensor && imgCensor){
                // 审核通过，修改状态和添加审核成功日志
                pet.setOnsaletime(new Date());
                pet.setState(1);
                petMapper.update(pet);

                // 添加日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(pet.getId());
                log.setState(1);
                log.setAuditTime(new Date());
                log.setNote("审核成功！");
                log.setAuditId(employee.getId());
                petOnlineAuditLogMapper.save(log);
            }else {
                // 审核失败
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(pet.getId());
                log.setState(-1);
                log.setAuditTime(new Date());
                log.setNote("审核失败，含有非法数据!");
                log.setAuditId(employee.getId());
                petOnlineAuditLogMapper.save(log);
                throw new BusinessException("审核失败，含有非法数据");
            }
        }
    }
}
